#ifndef DRAW_MINE_FIELD
#define DRAW_MINE_FIELD
//=========================================================
#include "operator_R.hpp"
//=========================================================
int a = 1; //dim of the cube edge
//=========================================================
//blending
void enable_blend(){
	glEnable(GL_BLEND);
  glBlendFunc(GL_ONE,GL_ONE);
}
void disable_blend(){
	glDisable(GL_BLEND);
}
//=========================================================
// DRAW QUANTUM STATE 
//=========================================================
void build_QS(float x,float y,float z,int a,int clr,int i){
  double k = 1.0/dim_R;
  if(clr == 1) glColor4f(1.0-k*i,0.0,0.0,1.0);
  if(clr == 5) glColor4f(1.0-k*i,1.0-k*i,0.0,1.0);
  if(clr == 0) glColor4f(0.0,1.0-k*i,0.0,1.0);
  glBegin(GL_QUADS);
  //back
    glVertex3f(x,y,z);
    glVertex3f(x,y+a,z);
    glVertex3f(x+a,y+a,z);
    glVertex3f(x+a,y,z);
  //front
    glVertex3f(x,y,z+a);
    glVertex3f(x,y+a,z+a);
    glVertex3f(x+a,y+a,z+a);
    glVertex3f(x+a,y,z+a);
  //bottom
    glVertex3f(x,y,z);
    glVertex3f(x,y,z+a);
    glVertex3f(x+a,y,z+a);
    glVertex3f(x+a,y,z);
  //top
    glVertex3f(x,y+a,z);
    glVertex3f(x,y+a,z+a);
    glVertex3f(x+a,y+a,z+a);
    glVertex3f(x+a,y+a,z);
  //left
  //if(left == true) glColor4f(0.0,0.0,1.0,1.0);
    glVertex3f(x,y,z);
    glVertex3f(x,y,z+a);
    glVertex3f(x,y+a,z+a);
    glVertex3f(x,y+a,z);
  //right
  //if(right == true) glColor4f(0.0,0.0,1.0,1.0);
    glVertex3f(x+a,y,z);
    glVertex3f(x+a,y,z+a);
    glVertex3f(x+a,y+a,z+a);
    glVertex3f(x+a,y+a,z);
  glEnd();
}
//=========================================================
// BUILD MINE-FIELD
//=========================================================
void build_R(R_matrix *R,int dim){
  bool right = false;
  bool left = false;
	for(int i=0; i<dim; i++){
		for(int j=0; j<R[i].dim_l; j++){
			for(int k=0; k<R[i].dim_c; k++){
        //blend in the middle and solid on corners
				// enable_blend();
				// left = false;
				// right = false;
				// if(k == 0) left = true;
				// if(k == R[i].dim_c-1) right = true;
				// if((k == R[i].dim_c-1)||(k == 0)) disable_blend();

        //blend all green
        if(R[i].r_m[j][k] == 0)
          enable_blend();
        else
          disable_blend();

        //blend all red
        // if(R[i].r_m[j][k] == 1)
        //   enable_blend();
        // else
        //   disable_blend();

        //draw only 0 and 5
        //if(R[i].r_m[j][k] != 1)
          build_QS(0.0+a*k,R[i].dim_l*a-a*j,0.0+a*i,a,R[i].r_m[j][k],i);
			}
		}
	}
}
//=========================================================
#endif